﻿using System;
using System.Configuration;
using System.Threading;
using System.Windows.Forms;

namespace PostProPr
{
    
    public partial class Form1 : Form
    {

        /// <summary>
        /// 定义一个线程，处理数据，并更新界面
        /// </summary>
        private Thread t = null;
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string cmd = ConfigurationManager.AppSettings["rds-fr-tool"];
            InitTask(sender, cmd);
        }

        private void RunTask(Object sender)
        {
           
             t = new Thread(new ParameterizedThreadStart(ExcuteCmd));
             t.Start(sender);
           
        }

        private static void ExcuteCmd(object sender)
        {
            try
            {
                Mysender s = (Mysender)sender;
                Button btn = (Button)s.Sender;
                btn.Enabled = false;
                DialogResult re = MessageBox.Show("Start", "Tips", MessageBoxButtons.YesNo);
                if (re == DialogResult.Yes)
                { 
                    //string str = @"cd D:\Package\rds-fr-tool-fork&git pull origin gui&git subtree pull --prefix=web/src/main/gui https://franklinc:syn+5213@git.synnex.org/scm/~franklinc/react-mycis-rdsfinereportreview.git master&git push origin gui";
                    string str = s.Cmd;
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.FileName = "cmd.exe";
                    p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
                    p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
                    p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
                    p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
                    p.StartInfo.CreateNoWindow = true;//不显示程序窗口
                    p.Start();//启动程序

                    //向cmd窗口发送输入信息
                    p.StandardInput.WriteLine(str + "&exit");

                    p.StandardInput.AutoFlush = true;
                    //p.StandardInput.WriteLine("exit");
                    //向标准输入写入要执行的命令。这里使用&是批处理命令的符号，表示前面一个命令不管是否执行成功都执行后面(exit)命令，如果不执行exit命令，后面调用ReadToEnd()方法会假死
                    //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令，后者表示必须前一个命令执行失败才会执行后面的命令



                    //获取cmd窗口的输出信息
                    string output = p.StandardOutput.ReadToEnd();

                    //StreamReader reader = p.StandardOutput;
                    //string line=reader.ReadLine();
                    //while (!reader.EndOfStream)
                    //{
                    //    str += line + "  ";
                    //    line = reader.ReadLine();
                    //}

                    p.WaitForExit();//等待程序执行完退出进程
                    p.Close();

                    btn.Enabled = true;
                    MessageBox.Show("Done");              } 




            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            string cmd = ConfigurationManager.AppSettings["ar-open-items"];
            InitTask(sender, cmd);
        }

        private void InitTask(object sender, string cmd)
        {
            var s = new Mysender {
                Cmd = cmd,
                Sender = (Object)sender
            };
            
            RunTask(s);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string cmd = ConfigurationManager.AppSettings["stellr-provision-ui-micro"];
            InitTask(sender, cmd); 
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string cmd = ConfigurationManager.AppSettings["sales-order"];
            InitTask(sender, cmd);
        }

        private void button5_Click(object sender, EventArgs e)
        {            
            string cmd = ConfigurationManager.AppSettings["storefront"];
            InitTask(sender, cmd);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string cmd = ConfigurationManager.AppSettings["test"];
            InitTask(sender, cmd);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string cmd = this.textBox1.Text; 
            InitTask(sender, cmd);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string cmd = ConfigurationManager.AppSettings["voicemal"];
            InitTask(sender, cmd);

        }

        private void button9_Click(object sender, EventArgs e)
        {
            string cmd = ConfigurationManager.AppSettings["redhat"];
            InitTask(sender, cmd);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            string cmd = ConfigurationManager.AppSettings["storefront4.7"];
            InitTask(sender, cmd);
        }
 

        private void button11_Click_1(object sender, EventArgs e)
        {
            string cmd = ConfigurationManager.AppSettings["loginPortal"];
            InitTask(sender, cmd);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            string cmd = ConfigurationManager.AppSettings["storefront4.6"];
            InitTask(sender, cmd);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            string cmd = ConfigurationManager.AppSettings["stellr-solution-ui-micro"];
            InitTask(sender, cmd);
        }

        private void button10_Click_1(object sender, EventArgs e)
        {
            string cmd = ConfigurationManager.AppSettings["stellr-portal-micro"];
            InitTask(sender, cmd);
        }
    }


    public class Mysender
    {
        public string Cmd { set; get; }
        public Object Sender { set; get; }

    }
}
